package com.zhkj.txg.module.mine.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.home.ui.ShopActivity
import com.zhkj.txg.module.mine.adapter.AfterSaleAdapter
import com.zhkj.txg.module.mine.vm.AfterSaleViewModel
import com.zhkj.txg.module.order.ui.OrderRefundDetailActivity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import kotlinx.android.synthetic.main.activity_after_sale.*

class AfterSaleActivity : ViewModelActivity<AfterSaleViewModel>() {
    override fun setViewModel(): AfterSaleViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleAfterSale
    override val layoutResId: Int = R.layout.activity_after_sale

    private val requestCodeStatusChange = 1092
    private val afterSaleAdapter = AfterSaleAdapter().apply {
        loadMoreModule?.setOnLoadMoreListener { viewModel.list() }
        setOnItemClickListener { _, _, position ->
            val order = data[position]
            OrderRefundDetailActivity.open(
                this@AfterSaleActivity,
                order.id,
                order.orderId,
                order.goodsId,
                requestCodeStatusChange
            )
        }
        setOnItemChildClickListener { _, view, position ->
            val order = data[position]
            when (view.id) {
                // 打开店铺
                R.id.tvAfterSaleShopName -> order.shop?.let {
                    ShopActivity.open(this@AfterSaleActivity, it.shopName, it.id)
                }
                R.id.ivAfterSaleShop -> order.shop?.let {
                    ShopActivity.open(this@AfterSaleActivity, it.shopName, it.id)
                }
                // 打开商品
                R.id.ivAfterSaleProduct -> order.goods?.let {
                    ProductDetailActivity.normalProduct(this@AfterSaleActivity, it.goodsId.toInt())
                }
            }
        }
    }

    override fun initUI() {
        with(rvAfterSale) {
            layoutManager = LinearLayoutManager(this@AfterSaleActivity)
            addItemDecoration(DividerItemDecoration(this@AfterSaleActivity, DividerItemDecoration.VERTICAL).apply {
                getDrawable(R.drawable.divider_vertical_1)?.let {
                    setDrawable(it)
                }
            })
            adapter = afterSaleAdapter
        }
        afterSaleAdapter.setEmptyView(R.layout.layout_empty_data)
        refreshAfterSale.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.list(true)
    }

    override fun initViewModel() {
        viewModel.refundListLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshAfterSale.finishRefresh()
                    else if (it.hasMore) afterSaleAdapter.loadMoreModule?.loadMoreComplete()
                    else afterSaleAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) afterSaleAdapter.addData(it.data)
                    else afterSaleAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) afterSaleAdapter.loadMoreModule?.loadMoreFail()
                    else refreshAfterSale.finishRefresh(false)
                }
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == requestCodeStatusChange) {
            viewModel.list(true)
            rvAfterSale.scrollTo(0, 0)
        }
    }
}